package gui;

import javax.swing.JPanel;

import Entities.RequestEntity;
import client.ChatClient;
import common.ChatIF;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 
 * @author Idan Mann
 * This GUI Class presents the user with a form where it can request a change in a supported system.
 * The form loads the user's data form the database and presents it as well.
 * 
 *
 */
public class RequestChangePanel extends JPanel implements ChatIF {
	String pid;
	private AcademicFrame frame;
	private InitiatorPanel initiatorPanel;
	private JTextField textFieldFN;
	private JTextField textFieldLN;
	private JTextField textFieldEmail;
	private JTextField textFieldTitle;
	private JTextField textFieldDep;
	private JTextField textFieldDate;
	private JTextArea textAreaCurrent;
	private JTextArea textAreaCD;
	private JTextArea textAreaExplanation;
	private JTextArea textAreaRemarks;
	private JButton btnCancel;
	private JButton btnAddFile;
	private JButton btnSubmit;
	private JCheckBox chckbxRemarks;
	private JComboBox<String> comboBoxSystem;
	ArrayList<Object> info;
	ArrayList<Object> message;
	RequestEntity request;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JScrollPane scrollPane_2;
	private JScrollPane scrollPane_3;
	public int fileLength=0;
	public byte[] bytes;
	//private static String[] list;
	

		public RequestChangePanel(AcademicFrame frame,InitiatorPanel initiatorPanel, String pid) {
			super();
			this.pid=pid;
			this.frame=frame;
			this.initiatorPanel=initiatorPanel;
			frame.setSize(500, 400);
			//this.list = new String[1];

			initialize();
			
			
		}
		private void initialize()
		{
		setLayout(null);
		
		JLabel titlelabel = new JLabel("Request Change");
		titlelabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		titlelabel.setForeground(Color.BLUE);
		titlelabel.setBounds(287, 21, 169, 34);
		add(titlelabel);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(10, 74, 73, 14);
		add(lblFirstName);
		
		textFieldFN = new JTextField();
		textFieldFN.setEditable(false);
		textFieldFN.setBounds(79, 71, 110, 20);
		add(textFieldFN);
		textFieldFN.setColumns(10);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(219, 74, 68, 14);
		add(lblLastName);
		
		textFieldLN = new JTextField();
		textFieldLN.setEditable(false);
		textFieldLN.setBounds(297, 71, 144, 20);
		add(textFieldLN);
		textFieldLN.setColumns(10);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(490, 74, 46, 14);
		add(lblEmail);
		
		textFieldEmail = new JTextField();
		textFieldEmail.setEditable(false);
		textFieldEmail.setBounds(530, 71, 162, 20);
		add(textFieldEmail);
		textFieldEmail.setColumns(10);
		
		JLabel lblTitle = new JLabel("Title");
		lblTitle.setBounds(10, 123, 46, 14);
		add(lblTitle);
		
		textFieldTitle = new JTextField();
		textFieldTitle.setEditable(false);
		textFieldTitle.setBounds(79, 120, 110, 20);
		add(textFieldTitle);
		textFieldTitle.setColumns(10);
		
		JLabel lblDepartment = new JLabel("Department");
		lblDepartment.setBounds(219, 123, 68, 14);
		add(lblDepartment);
		
		textFieldDep = new JTextField();
		textFieldDep.setEditable(false);
		textFieldDep.setBounds(297, 120, 144, 20);
		add(textFieldDep);
		textFieldDep.setColumns(10);
		
		JLabel lblDate = new JLabel("Date");
		lblDate.setBounds(490, 123, 30, 14);
		add(lblDate);
		
		textFieldDate = new JTextField();
		textFieldDate.setEditable(false);
		textFieldDate.setBounds(530, 120, 110, 20);
		add(textFieldDate);
		textFieldDate.setColumns(10);
		
		JLabel lblCurrentStatus = new JLabel("Current Status");
		lblCurrentStatus.setBounds(10, 201, 97, 14);
		add(lblCurrentStatus);
		
		JLabel lblSystem = new JLabel("System");
		lblSystem.setBounds(219, 163, 62, 14);
		add(lblSystem);
		
		comboBoxSystem = new JComboBox();
		comboBoxSystem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) {
			}
		});
		comboBoxSystem.setBounds(287, 159, 169, 22);
		add(comboBoxSystem);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(156, 201, 484, 73);
		add(scrollPane);
		
		textAreaCurrent = new JTextArea();
		scrollPane.setViewportView(textAreaCurrent);
		textAreaCurrent.setColumns(10);
		
		JLabel lblChangeDescription = new JLabel("Change Description");
		lblChangeDescription.setBounds(10, 299, 125, 14);
		add(lblChangeDescription);
		
		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(156, 294, 484, 73);
		add(scrollPane_1);
		
		textAreaCD = new JTextArea();
		scrollPane_1.setViewportView(textAreaCD);
		textAreaCD.setColumns(10);
		
		JLabel lblExplanation = new JLabel("Explanation");
		lblExplanation.setBounds(10, 393, 97, 14);
		add(lblExplanation);
		
		scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(157, 387, 483, 73);
		add(scrollPane_2);
		
		textAreaExplanation = new JTextArea();
		scrollPane_2.setViewportView(textAreaExplanation);
		textAreaExplanation.setColumns(10);
		
		chckbxRemarks = new JCheckBox("Remarks");
		chckbxRemarks.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if (chckbxRemarks.isSelected())
				{
					textAreaRemarks.setEditable(true);
					textAreaRemarks.setBackground(Color.white);
				}
				else
				{
					float[] fakeHSB = {0,0,0};
					textAreaRemarks.setText("");
					textAreaRemarks.setEditable(false);
					textAreaRemarks.setBackground(new Color (238, 238, 238));
				}
			}
		});
		chckbxRemarks.setBounds(10, 483, 97, 23);
		add(chckbxRemarks);
		
		scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(157, 484, 483, 73);
		add(scrollPane_3);
		
		textAreaRemarks = new JTextArea();
		textAreaRemarks.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
		textAreaRemarks.setEditable(false);
		scrollPane_3.setViewportView(textAreaRemarks);
		textAreaRemarks.setColumns(10);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if (textAreaCurrent.getText().isEmpty() || textAreaCD.getText().isEmpty() || textAreaExplanation.getText().isEmpty() || (comboBoxSystem.getSelectedItem().toString()).equalsIgnoreCase("--Choose a System--") )
				{
					(new Dialog("Please fill the form correctly.")).setVisible(true);
				}
				else
				{
					message = new ArrayList<Object>();
					request= new RequestEntity(textAreaCurrent.getText(),textAreaCD.getText(),textAreaExplanation.getText(),textAreaRemarks.getText(),textFieldFN.getText(),textFieldLN.getText(),textFieldDate.getText(),textFieldTitle.getText(),textFieldEmail.getText(),null, pid, comboBoxSystem.getSelectedItem().toString());
					//*file name set to null*//
					message.add("Request");
					message.add(request);
					message.add(bytes);
					message.add(fileLength);
					
					AcademicFrame.myclient.sendMessageToServer(message);					
				}
				
			}
		});
		btnSubmit.setBounds(476, 609, 91, 23);
		add(btnSubmit);
		
		btnAddFile = new JButton("Add File");
		
		btnAddFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Object> fileMessage= new ArrayList<Object>();
				JFileChooser fc = new JFileChooser();
				fc.setAcceptAllFileFilterUsed(false);
				FileNameExtensionFilter filter3 = new FileNameExtensionFilter("PDF FILES", "pdf");
				fc.setFileFilter(filter3);
				 if (e.getSource() == btnAddFile) {	
				        int returnVal = fc.showOpenDialog(frame);
				        if (returnVal == JFileChooser.APPROVE_OPTION) {
				        	
				        	File file = fc.getSelectedFile();
							int len = (int) file.length();
							try {
								FileInputStream fis = new FileInputStream(file);
								bytes = new byte[len];
								fis.read(bytes, 0, len);
								fileLength=len;
								btnAddFile.setText(file.getName());
								
							} catch (IOException e1) {
								// TODO Auto-generated catch block
							//	e1.printStackTrace();
							}
				       
				           
				        }
				 }
			}
		});
		btnAddFile.setBounds(329, 609, 91, 23);
		add(btnAddFile);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setSize(500, 500);
				frame.setContentPane(initiatorPanel);	
			}
		});
		btnCancel.setBounds(187, 609, 91, 23);
		add(btnCancel);

	}

	@Override
	public void display(Object message)
	{
		//info.clear(); // clear the info array list
		ArrayList<Object> info = new ArrayList<Object>();
		info = (ArrayList<Object>)message;
			
		
			if (	((String)(info.get(0))).equalsIgnoreCase("userInfo")	)
			{
				
				
				//initiate date variable
				SimpleDateFormat DateFormat = new SimpleDateFormat("dd/MM/yyyy");
				DateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
				//Setup user information in the panel
				this.textFieldFN.setText((String)info.get(1));
				this.textFieldLN.setText((String)info.get(2));
				this.textFieldEmail.setText((String)info.get(3));
				this.textFieldTitle.setText((String)info.get(4));
				this.textFieldDep.setText((String)info.get(5));
				this.textFieldDate.setText( DateFormat.format(new Date()));		 
			}
		
			else
				if (	((String)(info.get(0))).equalsIgnoreCase("listOfSystems")	)
				{
					ArrayList<String> systemList = (ArrayList<String>)message; 
									
					int numberOfSystems = Integer.parseInt( (systemList.get(1)).toString() ); // get the number of systems in the list
					this.comboBoxSystem.addItem("--Choose a System--");
					for (int i=0; i < numberOfSystems ; i++ )
					{
						this.comboBoxSystem.addItem((String)systemList.get(i+2));  // add systems to the list
					}


				}
				else
					if (	((String)(info.get(0))).equalsIgnoreCase("returnedRequest")	)
					{
						ArrayList<String> replyFromServer = (ArrayList<String>)message; 
										
						if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("Valid") )
						{
							(new Dialog ("Your request was sent for assessment, thank you.")).setVisible(true);
							frame.setSize(500, 500);
							frame.setContentPane(initiatorPanel);	
						}
						else
							if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("Invalid") )
							{
								(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
								frame.setSize(500, 500);
								frame.setContentPane(initiatorPanel);	
							}
						

					}
			
	}
}
